#include <stdio.h>
#include "./LinkedStack.h"

// 初始化
Status InitStack(Stack *stack) {
    (*stack) = NULL;
    return OK;
}

// 销毁
Status DestroyStack(Stack *stack) {
    Node *curNode = NULL;
    while(curNode = (*stack)) {
        *stack = (*stack)->next;
        free(curNode);
    }
    return ERROR;
}

// 判断是否为空
int StackEmpty(Stack stack) {
    return stack == NULL;
}

// 求栈的长度
int StackLength(Stack stack) {
    Node *curNode = stack;
    int len = 0;
    while(curNode) {
        len++;
        curNode = curNode->next;
    }
    return len;
}

// 取栈顶元素
Status GetTop(Stack stack, ElemType *e) {
    if(stack == NULL) {
        *e = NULL;
        return OK;
    }
    *e = stack->data;
    return OK;
}

// 置空栈
Status ClearStack(Stack *stack) {
    Node *curNode = NULL;
    while(curNode = (*stack)) {
        *stack = (*stack)->next;
        free(curNode);
    }
    return ERROR;
}

// 压栈
Status Push(Stack *stack, ElemType e) {
    Node *tmp = (Node*)malloc(sizeof(Node));
    if(!tmp) {
        return ERROR;
    }
    tmp->data = e;
    tmp->next = (*stack)->next;
    *stack = tmp;
    return OK;
}

// 弹栈
Status Pop(Stack *stack, ElemType *e) {
    *e = (*stack)->data;
    (*stack) = (*stack)->next;
    return OK;
}